package kr.hkit.team.score.erp;

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.db.DbUtil;
import kr.hkit.myframework.uicomp.PanelInputUi;
import kr.hkit.team.score.dao.ScoreDAO;
import kr.hkit.team.score.dto.Score;
import kr.hkit.team.score.service.ScoreService;

public class MainFrame extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JButton btn1;
	private ScoreService inputSc;
	private ScoreDAO dao;
	
	private PanelInputUi pStdNum;
	private PanelInputUi pStdName;
	private PanelInputUi pKor;
	private PanelInputUi pEng;
	private PanelInputUi pMath;
	private JPanel inputPanel;
	private JPanel btnPanel;
	private JButton btn2;
	private JButton btn3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final Connection con= ConnectionFactory.getConnection();
					MainFrame frame = new MainFrame(con);
					frame.addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e) {
							super.windowClosing(e);
							DbUtil.close(con);
						}
					});
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame(Connection con) {
		setTitle("고등학교 학생별 성적처리 프로그램");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 222);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		
		inputPanel = new JPanel();
		contentPane.add(inputPanel);
		inputPanel.setLayout(new GridLayout(1, 0, 0, 0));
		
		pStdNum = new PanelInputUi("학        번 :");
		FlowLayout fl_pStdNum = (FlowLayout) pStdNum.getLayout();
		fl_pStdNum.setAlignment(FlowLayout.LEFT);
		pStdNum.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				showStdName(); 
				Enable();
			}
		});
		inputPanel.add(pStdNum);
		
		pStdName = new PanelInputUi("성        명 :");
		inputPanel.add(pStdName);
		
		pKor = new PanelInputUi("국어점수 :");
		FlowLayout fl_pKor = (FlowLayout) pKor.getLayout();
		fl_pKor.setAlignment(FlowLayout.LEFT);
		contentPane.add(pKor);
		
		pEng = new PanelInputUi("영어점수 :");
		FlowLayout fl_pEng = (FlowLayout) pEng.getLayout();
		fl_pEng.setAlignment(FlowLayout.LEFT);
		contentPane.add(pEng);
		
		pMath = new PanelInputUi("수학점수 :");
		FlowLayout fl_pMath = (FlowLayout) pMath.getLayout();
		fl_pMath.setAlignment(FlowLayout.LEFT);
		contentPane.add(pMath);
		
		btnPanel = new JPanel();
		contentPane.add(btnPanel);
		
		btn1 = new JButton("입력");
		btn1.addActionListener(this);
		btnPanel.add(btn1);
		
		btn2 = new JButton("출력");
		btnPanel.add(btn2);
		
		btn3 = new JButton("전체삭제");
		btnPanel.add(btn3);
		
		dao = new ScoreDAO(con);
	}
	
	private Score getSc(){
   		int stdNum = Integer.parseInt(pStdNum.getTf().getText().trim());
   		int kor = Integer.parseInt(pKor.getTf().getText().trim());
   		int eng = Integer.parseInt(pEng.getTf().getText().trim());
   		int math = Integer.parseInt(pMath.getTf().getText().trim());
   		
		return new Score(stdNum, kor, eng, math);
	}
	
	private void Diable() {
		pStdNum.setTextField("");
		pStdName.getTf().setEnabled(true);
		pStdName.setTextField("");
		pKor.setTextField("");
		pEng.setTextField("");
		pMath.setTextField("");
	}
	
	private void Enable(){
		pStdName.getTf().setEnabled(false);
	}
	
	private void showStdName() { // 포커스아웃용
		
		int stdNum = Integer.parseInt(pStdNum.getTf().getText().trim());
		Score s = new Score();
		s.setStdNum(stdNum);

		try {
			s = dao.searchItems(s);
			pStdName.setTextField(s.getStdName());
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(this, "쿼리오류");
		} 
	}

	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn1) {
			actionPerformedBtn1(e);
		}
	}
	protected void actionPerformedBtn1(ActionEvent e) { //입력
		
		inputSc = new ScoreService(dao);
		Score sc = getSc();
		inputSc.insertData(sc);
		Diable();
		
	}
}
